#include "stdafx.h"
#include "AdoTool.h"
#include <Shlwapi.h>

CAdoTool::CAdoTool(CPatternSetterDlg *pDlg)
{
	m_bSet = FALSE;
	m_bResult = FALSE;
	m_pDlg = pDlg;
}

CAdoTool::~CAdoTool(void)
{
	Disconnect();
}

BOOL CAdoTool::GetIniInfo()
{
	BOOL bRet = TRUE;


	TCHAR strProfilePath[MAX_PATH];
	GetModuleFileName(NULL,strProfilePath,sizeof(strProfilePath)/sizeof(TCHAR));
	PathRemoveFileSpec(strProfilePath);
	TCHAR strFileName[] =  _T("odbc.ini");
	PathAppend(strProfilePath,strFileName);
	//AfxMessageBox(strProfilePath);

	TCHAR ch[_MAX_PATH + 1] = { 0 };
	if(::GetPrivateProfileString(_T("ODBC"), _T("servicename"), _T("error info"), 
		ch, sizeof(ch) - 1, strProfilePath))
	{
		// check error
		if(wcscmp(ch, _T("error info")) == 0)
		{
			m_strDSN = _T("aidem_west");
			bRet = FALSE;
			return bRet;
		}
		else
		{
			m_strDSN = ch;
		}
	}
	if(::GetPrivateProfileString(_T("ODBC"), _T("user"), _T("error info"), 
		ch, sizeof(ch) - 1, strProfilePath))
	{
		// check error
		if(wcscmp(ch, _T("error info")) == 0)
		{
			m_strUser = _T("wakutori");
			bRet = FALSE;
			return bRet;
		}
		else
		{
			m_strUser = ch;
		}
	}
	if(::GetPrivateProfileString(_T("ODBC"), _T("password"), _T("error info"), 
		ch, sizeof(ch) - 1, strProfilePath))
	{
		// check error
		if(wcscmp(ch, _T("error info")) == 0)
		{
			m_strPwd = _T("wakutori");
			bRet = FALSE;
			return bRet;
		}
		else
		{
			m_strPwd = ch;
		}
	}
	return bRet;
}

void CAdoTool::GetXiaoSizeList(CObArray &arrXiaoSizeList)
{
	Connect();
	if(!m_bSet)
		return ;

	arrXiaoSizeList.RemoveAll();
	HRESULT hr;
	_CommandPtr pCmd;
	hr = pCmd.CreateInstance(__uuidof(Command));
	pCmd->ActiveConnection = m_pConn;
	pCmd->CommandType = adCmdText;

	CString strCommand=_T("select m.WAKU_SIZE_ID,m.WAKU_SIZE_NAME || '(' || m.WAKU_SIZE_CODE || ')' WAKU_SIZE_NAME,m.ROW_WAKU_SIZE,m.COL_WAKU_SIZE,m.WAKU_SIZE_CODE	from m_waku_size m	where m.MEDIA_ID = 184");
	pCmd->CommandText = strCommand.operator LPCTSTR();
	int nCount;
	_RecordsetPtr pRst = NULL;
	try
	{
		pRst = pCmd->Execute(NULL, NULL, adCmdText);  
		/********Warning:if you want to get recordset, the output value of storeProc will be empty!
		Why? I do not know.:--(
		No, now I solved the problem.
		*****************************************/	
		int nFieldCount = pRst->Fields->Count;
		nCount = 0;			
		PXIAOSIZEINFO pInfoTemp;

		while(!(pRst->EndOfFile))
		{	
			pInfoTemp = new XIAOSIZEINFO;
			nCount++;
			Fields * fields = NULL;
			hr = pRst->get_Fields(&fields);
			for(long i = 0; i < nFieldCount; i++)
			{
				if (i == 0)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						int nValue = fields->Item[i]->Value.intVal;
						pInfoTemp->WAKU_SIZE_ID.Format(L"%d",nValue);
					}
					else
					{
						pInfoTemp->WAKU_SIZE_ID = _T("");
					}
				}
				else if (i == 1)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						pInfoTemp->WAKU_SIZE_NAME = fields->Item[i]->Value.bstrVal;
					}
					else
					{
						pInfoTemp->WAKU_SIZE_NAME = _T("");
					}
				}
				else if (i == 2)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						int nValue = fields->Item[i]->Value.intVal;
						pInfoTemp->ROW_WAKU_SIZE.Format(L"%d",nValue);
					}
					else
					{
						pInfoTemp->ROW_WAKU_SIZE = _T("");
					}
				}
				else if (i == 3)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						int nValue = fields->Item[i]->Value.intVal;
						pInfoTemp->COL_WAKU_SIZE.Format(L"%d",nValue);
					}
					else
					{
						pInfoTemp->COL_WAKU_SIZE = _T("");
					}
				}
				else if (i == 4)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						int nValue = fields->Item[i]->Value.intVal;
						pInfoTemp->WAKU_SIZE_CODE.Format(L"%d",nValue);
					}
					else
					{
						pInfoTemp->WAKU_SIZE_CODE = _T("");
					}
				}
			}

			arrXiaoSizeList.Add((CObject *)pInfoTemp);
			pRst->MoveNext();         
		}

		//Release the IADORecordset Interface here        
		pRst->Close();
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//PrintProviderError(m_pConn);

		CString strInfo;
		strInfo.Format(_T("\n Source : %s \n Description : %s \n"),(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
		AfxMessageBox(strInfo);
		Disconnect();
	}
}

void CAdoTool::GetDaSizeList(CObArray &arrDaSizeList)
{
	Connect();
	if(!m_bSet)
		return ;

	arrDaSizeList.RemoveAll();
	HRESULT hr;
	_CommandPtr pCmd;
	hr = pCmd.CreateInstance(__uuidof(Command));
	pCmd->ActiveConnection = m_pConn;
	pCmd->CommandType = adCmdText;

	CString strCommand=_T("select t.hakkou_size_group_name, t.hakkou_size_group_id, t.page_num from h_hakkou_size_group t	where t.hakkou_size_group_id <> 13	order by page_num asc");
	pCmd->CommandText = strCommand.operator LPCTSTR();
	int nCount;
	_RecordsetPtr pRst = NULL;
	try
	{
		pRst = pCmd->Execute(NULL, NULL, adCmdText);  
		/********Warning:if you want to get recordset, the output value of storeProc will be empty!
		Why? I do not know.:--(
		No, now I solved the problem.
		*****************************************/	
		int nFieldCount = pRst->Fields->Count;
		nCount = 0;			
		PDASIZEINFO pInfoTemp;

		while(!(pRst->EndOfFile))
		{	
			pInfoTemp = new DASIZEINFO;
			nCount++;
			Fields * fields = NULL;
			hr = pRst->get_Fields(&fields);
			for(long i = 0; i < nFieldCount; i++)
			{
				if (i == 0)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						pInfoTemp->SIZE_GROUP_NAME = fields->Item[i]->Value.bstrVal;
					}
					else
					{
						pInfoTemp->SIZE_GROUP_NAME = _T("");
					}
				}
				else if (i == 1)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						int nValue = fields->Item[i]->Value.intVal;
						pInfoTemp->SIZE_GROUP_ID.Format(L"%d",nValue);
					}
					else
					{
						pInfoTemp->SIZE_GROUP_ID = _T("");
					}
				}
				else if (i == 2)
				{
					if(fields->Item[i]->Value.vt != VT_NULL)
					{
						int nValue = fields->Item[i]->Value.intVal;
						pInfoTemp->PAGE_NUM.Format(L"%d",nValue);
					}
					else
					{
						pInfoTemp->PAGE_NUM = _T("");
					}
				}
			}

			arrDaSizeList.Add((CObject *)pInfoTemp);
			pRst->MoveNext();         
		}

		//Release the IADORecordset Interface here        
		pRst->Close();
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//PrintProviderError(m_pConn);

		CString strInfo;
		strInfo.Format(_T("\n Source : %s \n Description : %s \n"),(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
		AfxMessageBox(strInfo);
		Disconnect();
	}
}

void CAdoTool::Disconnect()
{
	if(m_bSet /*&& m_pConn->State*/)
	{
		try
		{
			m_pConn->Close();
		}
		catch(_com_error &e)
		{
		}
		catch(...)
		{
		}
		m_pConn = NULL;
		m_bSet = FALSE;
	}
}

BOOL CAdoTool::Connect(void)
{
	BOOL bRet = TRUE;
	HRESULT hr;

	if(m_bSet)
	{
		try
		{
			if(m_pConn->State)
				return bRet;
			else
			{
				Disconnect();
			}
		}
		catch(_com_error &e)
		{	
			Disconnect();
		}
		catch(...)
		{
			Disconnect();
		}
	}

	if (!GetIniInfo())
	{
		bRet = FALSE;
		return bRet;
	}
	

	try
	{
		_bstr_t strMissing(L""); 
		hr = m_pConn.CreateInstance(__uuidof(Connection));		
		CString strConnectionString = "Provider=OraOLEDB.Oracle;Data Source="+ m_strDSN +
			"; User Id=" + m_strUser +
			";Password="+ m_strPwd ;
		m_pConn->ConnectionString = strConnectionString.operator LPCTSTR();
		hr = m_pConn->Open(strMissing, 
			strMissing, 
			strMissing, -1);

		if(hr == 0)
		{
			bRet = TRUE;
			m_bSet = TRUE;
		}
		else
			bRet = FALSE;
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//PrintProviderError(m_pConn);

		CString strInfo;
		strInfo.Format(_T("\n Source : %s \n Description : %s \n"),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription);
		AfxMessageBox(strInfo);
		bRet = FALSE;
	}

	return bRet;
}

void CAdoTool::InsertPattern( int nGroupId,int nType,vector<CLabelEx*> &arrLabels,int nEnablePages)
{
	Connect();
	if(!m_bSet)
		return ;

	HRESULT hr;
	_CommandPtr pCmd;
	hr = pCmd.CreateInstance(__uuidof(Command));
	pCmd->ActiveConnection = m_pConn;
	pCmd->CommandType = adCmdText;

	CString strCommand=_T("select  SEQ_HAKKOUPATTERN_ID.nextval from dual");
	pCmd->CommandText = strCommand.operator LPCTSTR();
	int nCount;
	int nPatternId = 0; 
	_RecordsetPtr pRst = NULL;
	try
	{
		pRst = pCmd->Execute(NULL, NULL, adCmdText);  
		/********Warning:if you want to get recordset, the output value of storeProc will be empty!
		Why? I do not know.:--(
		No, now I solved the problem.
		*****************************************/	
		int nFieldCount = pRst->Fields->Count;
		nCount = 0;

		if(!(pRst->EndOfFile))
		{
			Fields * fields = NULL;
			hr = pRst->get_Fields(&fields);
			nPatternId = fields->Item["nextval"]->Value.intVal;
		}
		//Release the IADORecordset Interface here        
		pRst->Close();
		pRst = NULL;
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//PrintProviderError(m_pConn);

		CString strInfo;
		strInfo.Format(_T("\n Source : %s \n Description : %s \n"),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription);
		AfxMessageBox(strInfo);
		Disconnect();
		return;
	}
	
	try
	{
		m_pConn->BeginTrans();
		strCommand.Format(_T("insert into SH_HAKKOUPATTERN_MAIN(PATTERN_ID,HAKKOU_SIZE_GROUP_ID,ENCHOU_PAGE_NUM\
							 ,TOKUSYUU1_PAGE_NUM,TOKUSYUU2_PAGE_NUM,CREATEDATE) values(%d,%d,%d,%d,%d,to_char(sysdate, 'yyyy-mm-dd'))"),nPatternId,nGroupId,0,0,0);
		pCmd->CommandText = strCommand.operator LPCTSTR();
		pCmd->Execute(NULL, NULL, adCmdText);


		int nWakuSizeId,nPageFlag,nMianFlag,nXPoint,nYPoint;
		vector<CLabelEx*>::iterator iter;
		int nsize = arrLabels.size();		
		for (iter = arrLabels.begin();iter!=arrLabels.end();iter++)
		{	
			nPageFlag = _wtol( (*iter)->m_strPageFlag );
			nMianFlag = _wtol( (*iter)->m_strMianId );

			if (nMianFlag > nEnablePages)
			{
				continue;
			}

			if (nEnablePages == 1)
			{
				if ( nPageFlag == 2)
				{
					nMianFlag += 1;

				}
				nPageFlag=3;
			}
			else if (nEnablePages == 3 && nMianFlag == 3)
			{		
				if ( nPageFlag == 2)
					nMianFlag += 1;
				nPageFlag=3;
			}
			else if (nEnablePages == 5 && nMianFlag == 5)
			{
				if ( nPageFlag == 2)
					nMianFlag += 1;
				nPageFlag=3;
			}
			else if (nEnablePages == 7 && nMianFlag == 7)
			{
				if ( nPageFlag == 2)
					nMianFlag += 1;
				nPageFlag=3;
			}
			else if (nEnablePages == 9 && nMianFlag == 9)
			{
				if ( nPageFlag == 2)
					nMianFlag += 1;
				nPageFlag=3;
			}
			else if (nEnablePages == 11 && nMianFlag == 11)
			{
				if ( nPageFlag == 2)
					nMianFlag += 1;
				nPageFlag=3;
			}


			nWakuSizeId = _wtol( (*iter)->m_strSizeId );

			nXPoint = (*iter)->m_x + 1;
			nYPoint = (*iter)->m_y + 1;

			strCommand.Format(_T("insert into SH_HAKKOUPATTERN_CHILD(PATTERN_ID,WAKU_SIZE_ID,MEN_FLAG,MEN_SORT_NUM\
								 ,POINT_X,POINT_Y) values(%d,%d,%d,%d,%d,%d)"),nPatternId,nWakuSizeId,nPageFlag,nMianFlag,nXPoint,nYPoint);
			pCmd->CommandText = strCommand.operator LPCTSTR();
			pCmd->Execute(NULL, NULL, adCmdText);

			

		}
		
		m_pConn->CommitTrans();
		m_bResult = TRUE;
		m_pDlg->m_bChanged = FALSE;
	}
	catch(_com_error &e)
	{
		m_bResult = FALSE;
		m_pDlg->m_bChanged = TRUE;

		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//PrintProviderError(m_pConn);

		CString strInfo;
		strInfo.Format(_T("\n Source : %s \n Description : %s \n"),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription);
		AfxMessageBox(strInfo);
		Disconnect();
		return;
	}
}